import java.io.File;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.HashMap;

import org.apache.commons.io.FileUtils;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class JSONConverter {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		if (args.length<=0)
		{
			System.out.print("You mast past path for JSON file as input");
			System.exit(-1);
		}
		String jsonContent = null;
		try {
			jsonContent=FileUtils.readFileToString(new File(JSONConverter.class.getResource(args[0]).getPath()));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (jsonContent == null)
			return;
		Gson gson = new Gson();
		
		Type collectionType = new TypeToken<Collection<MavenSegment>>(){}.getType();
		Collection<MavenSegment> mavenSegmentList = gson.fromJson(jsonContent, collectionType);
		
		HashMap<String, Double> mapping = new HashMap<String,Double>();
		for (MavenSegment ms : mavenSegmentList){
			for (Mojo mojo : ms.getMojos()){
				if (!mapping.containsKey(mojo.mojo)){
					mapping.put(mojo.mojo,mojo.duration/1000);
				}
				else
				{
					mapping.put(mojo.mojo,mapping.get(mojo.mojo)+(mojo.duration/1000));
				}
			}
		}
		for (String key : mapping.keySet())
			System.out.println(key+","+mapping.get(key));
	}

}
